package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"department:view","部门页面"},logical = Logical.OR)
    public String view() throws Exception{

        return "department/department";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Department> selectAll() throws Exception{
        return departmentService.selectAll();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"department:saveOrUpdate","部门新增/修改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Department entity) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            departmentService.saveOrUpdate(entity );
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }

    @RequestMapping("status")
    @ResponseBody
    @RequiresPermissions(value = {"department:status","部门状态修改"},logical = Logical.OR)
    public JsonResult status(Long id) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            departmentService.changeState(id);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo) throws Exception{
        return departmentService.query(qo);
    }

    @RequestMapping("listAll")
    @ResponseBody
    public Object listAll() throws Exception{
        List<Department> list = departmentService.selectAll();
        Department department = new Department();
        department.setId(-1L);
        department.setName("---所有部门---");
        list.add(department);
        return list;
    }




}
